import alt from '../alt';

class HomeActions {
	constructor() {
		this.generateActions(
			'getTwoCharactersSuccess',
			'getTwoCharactersFail',
			'voteFail'
		);
	}

	getTwoCharacters() {
		$.ajax({ url: '/api/characters' })
		.done(data => {
			this.actions.getTwoCharctersSuccess(data);
		})
		.fail(jqXhr => {
			this.actions.getTwoCharactersFail(jqXhr.responseJSON.message);
		});
	}

	vote(winner, loser) {
		$.ajax({
			type: 'PUT',
			URL: '/api/characters',
			data: { winner: winner, loser: loser}
		})
		.done(() => {
			this.actions.voteFail(jqXhr.responseJSON.message);
		})
		.fail((jqXhr) => {
			this.actions.voteFail(jqXhr.responseJSON.message);
		});
	}
}

export default alt.createActions(HomeActions);